<?php
	include("includes/connect_db.php");
    $id_outbox = $_POST['id_outbox'];
    $aksi = $_GET['aksi'];
    $isi_text = $_POST['reply_msg'];

    switch ($aksi) {
    	case '2':
			    $pesan = $isi_text;

    			$jmlSMS = ceil(strlen($pesan)/153);
				$pecah  = str_split($pesan, 153);
				
				$query = "SHOW TABLE STATUS LIKE 'outbox'";
				$hasil = mysql_query($query);
				$data  = mysql_fetch_array($hasil);
				$newID = $data['Auto_increment'];

			    $sql_cari_nomer = "select DestinationNumber from sentitems where ID = '$id_outbox'";
			    $query_cari_nomer = mysql_query($sql_cari_nomer);
			    $cari_nomer = mysql_fetch_array($query_cari_nomer);
			    $nomor_kirim = $cari_nomer['DestinationNumber'];

			    if ($jmlSMS == 1) {
					$sql_kirim_pesan = "insert into outbox 
				    						(TextDecoded, DestinationNumber, CreatorID) 
				    					values 
				    						('$pesan', '$nomor_kirim', '')";

				    if ($query_kirim_pesan = mysql_query($sql_kirim_pesan)){
				    	echo "sukses";
				    } 
				} elseif ($jmlSMS > 1) {
					for ($i=1; $i<=$jmlSMS; $i++) {
					   	$udh = "050003A7".sprintf("%02s", $jmlSMS).sprintf("%02s", $i);

					   	$msg = $pecah[$i-1];

					   	if ($i == 1) {
					      	$sql_kirim_pesan = "INSERT INTO outbox (DestinationNumber, UDH, TextDecoded, ID, MultiPart, CreatorID)
					                			VALUES ('$nomor_kirim', '$udh', '$msg', '$newID', 'true', '')";

					   	} else { 
					      	$sql_kirim_pesan = "INSERT INTO outbox_multipart(UDH, TextDecoded, ID, SequencePosition)
					                			VALUES ('$udh', '$msg', '$newID', '$i')";
					   	} 
						
						if ($query_kirim_pesan = mysql_query($sql_kirim_pesan)){
					    	$bantu = 1;
					    } else{	
					    	$bantu = 0;
				    	}
					}

					if ($bantu == 1) {
						echo "sukses";
					}
				}	
    		break;

    	case '3':
    			$sql_delete = "DELETE FROM sentitems WHERE ID = '$id_outbox'";
				$query_delete = mysql_query($sql_delete);
				if($query_delete){
					echo "sukses";
				}
    		break;

    	case '4':
    			$sql_cari_nomer = "SELECT DestinationNumber FROM sentitems WHERE ID = '$id_outbox'";
				$query_cari_nomer = mysql_query($sql_cari_nomer);
				$cari_nomer = mysql_fetch_array($query_cari_nomer);

				$sql_delete_inbox = "DELETE FROM inbox WHERE SenderNumber = '$cari_nomer[DestinationNumber]'";
				$sql_delete_outbox = "DELETE FROM sentitems WHERE DestinationNumber = '$cari_nomer[DestinationNumber]'";

				$sql_jumlah_inbox = "SELECT * FROM inbox WHERE SenderNumber = '$cari_nomer[DestinationNumber]'";
				$sql_jumlah_outbox = "SELECT * FROM sentitems WHERE DestinationNumber = '$cari_nomer[DestinationNumber]'";
				$query_jumlah_inbox = mysql_query($sql_jumlah_inbox);
				$query_jumlah_outbox = mysql_query($sql_jumlah_outbox);
				$jumlah_inbox = mysql_num_rows($query_jumlah_inbox);
				$jumlah_outbox = mysql_num_rows($query_jumlah_outbox);

				$query_delete_inbox = false;
				$query_delete_outbox = false;

				if ($jumlah_inbox > 0) {
					$query_delete_inbox = mysql_query($sql_delete_inbox);
				}

				if ($jumlah_outbox > 0){
					$query_delete_outbox = mysql_query($sql_delete_outbox);
				}

				if($query_delete_inbox || $query_delete_outbox){
					echo "sukses";
				}
    		break;
    }
?>
